home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 001 / pibt40s3.arc / PIBFHIO.MOD < prev    next >
Text File  |  1986-11-03  |  21KB  |  413 lines

  1. (*----------------------------------------------------------------------*)
  2. (*        PIBFHIO.PAS --- File Handle Input/Output for Turbo Pascal     *)
  3. (*----------------------------------------------------------------------*)
  4. (*                                                                      *)
  5. (*  Author:  Philip R. Burns                                            *)
  6. (*  Version: 1.0  (June, 1985)                                          *)
  7. (*           2.0  (November, 1986)                                      *)
  8. (*                                                                      *)
  9. (*  Systems: For MS-DOS on IBM PCs and close compatibles only.          *)
  10. (*                                                                      *)
  11. (*  History: Original with me.                                          *)
  12. (*                                                                      *)
  13. (*           Suggestions for improvements or corrections are welcome.   *)
  14. (*           Please leave messages on Gene Plantz's BBS (312) 882 4145  *)
  15. (*           or Ron Fox's BBS (312) 940 6496.                           *)
  16. (*                                                                      *)
  17. (*           IF you use this code in your own programs, please be nice  *)
  18. (*           and give proper credit.                                    *)
  19. (*                                                                      *)
  20. (*----------------------------------------------------------------------*)
  21. (*                                                                      *)
  22. (*  Usage:   These routines provide a Turbo Pascal interface to the     *)
  23. (*           MS DOS file handle style input/output routines.  These are *)
  24. (*           used in PibTerm for I/O during uploading and downloading,  *)
  25. (*           as well as for copying files.  Use of these routines       *)
  26. (*           allows precise control of file sizes and avoids various    *)
  27. (*           bugs in the Turbo Block I/O routines.                      *)
  28. (*                                                                      *)
  29. (*----------------------------------------------------------------------*)
  30. (*                                                                      *)
  31. (*  Routines:                                                           *)
  32. (*                                                                      *)
  33. (*      Open_File_Handle                                                *)
  34. (*      Close_File_Handle                                               *)
  35. (*      Create_File_Handle                                              *)
  36. (*      Read_File_Handle                                                *)
  37. (*      Write_File_Handle                                               *)
  38. (*                                                                      *)
  39. (*----------------------------------------------------------------------*)
  40.  
  41. (*----------------------------------------------------------------------*)
  42. (*                  Constants for file handle access                    *)
  43. (*----------------------------------------------------------------------*)
  44.  
  45. FUNCTION Open_File_Handle(     File_Name:   AnyStr;
  46.                                File_Access: INTEGER;
  47.                            VAR File_Handle: INTEGER ) : INTEGER;
  48.  
  49. (*----------------------------------------------------------------------*)
  50. (*                                                                      *)
  51. (*     Function:   Open_File_Handle                                     *)
  52. (*                                                                      *)
  53. (*     Purpose:    Opens file using file handle                         *)
  54. (*                                                                      *)
  55. (*     Calling Sequence:                                                *)
  56. (*                                                                      *)
  57. (*        Error := Open_File_Handle(     File_Name:   AnyStr;           *)
  58. (*                                       File_Access: INTEGER;          *)
  59. (*                                   VAR File_Handle: INTEGER ) :       *)
  60. (*                                       INTEGER;                       *)
  61. (*                                                                      *)
  62. (*           File_Name   --- path name of file to be opened             *)
  63. (*           File_Access --- mode to access file                        *)
  64. (*                           0 = read only                              *)
  65. (*                           1 = write only                             *)
  66. (*                           2 = read and write                         *)
  67. (*           File_Handle --- returned file handle                       *)
  68. (*           Error       --- DOS error return code                      *)
  69. (*                                                                      *)
  70. (*     Calls:                                                           *)
  71. (*                                                                      *)
  72. (*        MsDos                                                         *)
  73. (*                                                                      *)
  74. (*----------------------------------------------------------------------*)
  75.  
  76. VAR
  77.    Reg  : RegPack;
  78.  
  79. BEGIN (* Open_File_Handle *)
  80.                                    (* Convert path name to Ascii Z string *)
  81.  
  82.    Convert_String_To_AsciiZ( File_Name );
  83.  
  84.                                    (* Set parameters for open file handle *)
  85.    Reg.Al := File_Access;
  86.    Reg.Ah := $3D;
  87.    Reg.Ds := SEG( File_Name[1] );
  88.    Reg.Dx := OFS( File_Name[1] );
  89.  
  90.                                    (* Open file, get handle *)
  91.    MsDos( Reg );
  92.                                    (* Check for bad return  *)
  93.  
  94.    IF ( Carry_Flag AND Reg.Flags ) = 0 THEN
  95.       BEGIN
  96.          Open_File_Handle := 0;
  97.          File_Handle      := Reg.Ax;
  98.       END
  99.    ELSE
  100.       BEGIN
  101.          Open_File_Handle := Reg.Ax;
  102.          File_Handle      := 0;
  103.       END;
  104.  
  105. END   (* Open_File_Handle *);
  106.  
  107. (*----------------------------------------------------------------------*)
  108. (*                Close_File_Handle --- Closes file handle              *)
  109. (*----------------------------------------------------------------------*)
  110.  
  111. FUNCTION Close_File_Handle( File_Handle: INTEGER ) : INTEGER;
  112.  
  113. (*----------------------------------------------------------------------*)
  114. (*                                                                      *)
  115. (*     Function:   Close_File_Handle                                    *)
  116. (*                                                                      *)
  117. (*     Purpose:    Closes file handle                                   *)
  118. (*                                                                      *)
  119. (*     Calling Sequence:                                                *)
  120. (*                                                                      *)
  121. (*        Error := Close_File_Handle( File_Handle: INTEGER ): INTEGER;  *)
  122. (*                                                                      *)
  123. (*           File_Handle --- File handle of file to close               *)
  124. (*           Error       --- DOS error return code                      *)
  125. (*                                                                      *)
  126. (*     Calls:                                                           *)
  127. (*                                                                      *)
  128. (*        MsDos                                                         *)
  129. (*                                                                      *)
  130. (*----------------------------------------------------------------------*)
  131.  
  132. VAR
  133.    Reg  : RegPack;
  134.  
  135. BEGIN (* Close_File_Handle *)
  136.  
  137.                                    (* Set parameters for close file handle *)
  138.    Reg.Ah := $3E;
  139.    Reg.Bx := File_Handle;
  140.                                    (* Close the file handle *)
  141.    MsDos( Reg );
  142.                                    (* Check for bad return  *)
  143.  
  144.    IF ( Carry_Flag AND Reg.Flags ) = 0 THEN
  145.       Close_File_Handle := 0
  146.    ELSE
  147.       Close_File_Handle := Reg.Ax;
  148.  
  149. END   (* Close_File_Handle *);
  150.  
  151. (*----------------------------------------------------------------------*)
  152. (*           Read_File_Handle --- Performs read using file handle       *)
  153. (*----------------------------------------------------------------------*)
  154.  
  155. FUNCTION Read_File_Handle(     File_Handle: INTEGER;
  156.                            VAR File_Buffer  ;
  157.                            VAR Read_Length: INTEGER ) : INTEGER;
  158.  
  159. (*----------------------------------------------------------------------*)
  160. (*                                                                      *)
  161. (*     Function:   Read_File_Handle                                     *)
  162. (*                                                                      *)
  163. (*     Purpose:    Reads file using file handle                         *)
  164. (*                                                                      *)
  165. (*     Calling Sequence:                                                *)
  166. (*                                                                      *)
  167. (*        Error := Read_File_Handle(     File_Handle: INTEGER;          *)
  168. (*                                   VAR File_Buffer  ;                 *)
  169. (*                                   VAR Read_Length: INTEGER ) :       *)
  170. (*                                           INTEGER;                   *)
  171. (*                                                                      *)
  172. (*           File_Handle --- File handle of file to read                *)
  173. (*           File_Buffer --- Buffer area to receive data read           *)
  174. (*           Read_Length --- On input, is number of characters to read. *)
  175. (*                           On output, is number of chars actually     *)
  176. (*                           read                                       *)
  177. (*           Error       --- DOS error return code                      *)
  178. (*                                                                      *)
  179. (*     Calls:                                                           *)
  180. (*                                                                      *)
  181. (*        MsDos                                                         *)
  182. (*                                                                      *)
  183. (*----------------------------------------------------------------------*)
  184.  
  185. VAR
  186.    Reg  : RegPack;
  187.  
  188. BEGIN (* Read_File_Handle *)
  189.                                    (* Set parameters for read *)
  190.    WITH Reg DO
  191.       BEGIN
  192.  
  193.          Ah := $3F;
  194.          Bx := File_Handle;
  195.          Cx := Read_Length;
  196.          Ds := SEG( File_Buffer );
  197.          Dx := OFS( File_Buffer );
  198.  
  199.       END;
  200.                                    (* Perform the read *)
  201.    MsDos( Reg );
  202.                                    (* Check for bad return  *)
  203.  
  204.    IF ( Carry_Flag AND Reg.Flags ) = 0 THEN
  205.       BEGIN
  206.          Read_File_Handle := 0;
  207.          Read_Length      := Reg.Ax;
  208.       END
  209.    ELSE
  210.       BEGIN
  211.          Read_File_Handle := Reg.Ax;
  212.          Read_Length      := 0;
  213.       END;
  214.  
  215. END   (* Read_File_Handle *);
  216.  
  217. (*----------------------------------------------------------------------*)
  218. (*  Relative_Position_File_Handle --- Position file relative to current *)
  219. (*----------------------------------------------------------------------*)
  220.  
  221. FUNCTION Relative_Position_File_Handle( File_Handle: INTEGER;
  222.                                         Offset     : INTEGER ) : INTEGER;
  223.  
  224. (*----------------------------------------------------------------------*)
  225. (*                                                                      *)
  226. (*     Function:   Relative_Position_File_Handle                        *)
  227. (*                                                                      *)
  228. (*     Purpose:    Positions file relative to current position          *)
  229. (*                                                                      *)
  230. (*     Calling Sequence:                                                *)
  231. (*                                                                      *)
  232. (*        Error := Relative_Position_File_Handle( File_Handle: INTEGER; *)
  233. (*                                                Offset     : INTEGER  *)
  234. (*                                              ) : INTEGER;            *)
  235. (*                                                                      *)
  236. (*           File_Handle --- File handle of file to read                *)
  237. (*           Offset      --- Byte offset relative to current position   *)
  238. (*                           to position file to.                       *)
  239. (*           Error       --- DOS error return code                      *)
  240. (*                                                                      *)
  241. (*     Calls:                                                           *)
  242. (*                                                                      *)
  243. (*        MsDos                                                         *)
  244. (*                                                                      *)
  245. (*----------------------------------------------------------------------*)
  246.  
  247. VAR
  248.    Reg  : RegPack;
  249.  
  250. BEGIN (* Relative_Position_File_Handle *)
  251.  
  252.                                    (* Set parameters for read *)
  253.    WITH Reg DO
  254.       BEGIN
  255.  
  256.          Ah := $42;
  257.          Al := 1;
  258.          Bx := File_Handle;
  259.          Dx := Offset;
  260.  
  261.          IF ( Offset > 0 ) THEN
  262.             Cx := 0
  263.          ELSE
  264.             Cx := -1;
  265.  
  266.       END;
  267.                                    (* Perform the read *)
  268.    MsDos( Reg );
  269.                                    (* Check for bad return  *)
  270.  
  271.    IF ( Carry_Flag AND Reg.Flags ) = 0 THEN
  272.       Relative_Position_File_Handle := 0
  273.    ELSE
  274.       Relative_Position_File_Handle := Reg.Ax;
  275.  
  276. END   (* Relative_Position_File_Handle *);
  277.  
  278. (*----------------------------------------------------------------------*)
  279. (*          Write_File_Handle --- Performs read using file handle       *)
  280. (*----------------------------------------------------------------------*)
  281.  
  282. FUNCTION Write_File_Handle(     File_Handle: INTEGER;
  283.                             VAR File_Buffer  ;
  284.                             VAR Write_Length: INTEGER ) : INTEGER;
  285.  
  286. (*----------------------------------------------------------------------*)
  287. (*                                                                      *)
  288. (*     Function:   Write_File_Handle                                    *)
  289. (*                                                                      *)
  290. (*     Purpose:    Writes file using file handle                        *)
  291. (*                                                                      *)
  292. (*     Calling Sequence:                                                *)
  293. (*                                                                      *)
  294. (*        Error := Write_File_Handle(     File_Handle: INTEGER;         *)
  295. (*                                    VAR File_Buffer ;                 *)
  296. (*                                    VAR Write_Length: INTEGER ):      *)
  297. (*                                            INTEGER;                  *)
  298. (*                                                                      *)
  299. (*           File_Handle  --- File handle of file to write              *)
  300. (*           File_Buffer  --- Buffer area from which to write data      *)
  301. (*           Write_Length --- On input, is number of chars. to write.   *)
  302. (*                            On output, is number of chars actually    *)
  303. (*                            written.                                  *)
  304. (*           Error        --- DOS error return code                     *)
  305. (*                                                                      *)
  306. (*     Calls:                                                           *)
  307. (*                                                                      *)
  308. (*        MsDos                                                         *)
  309. (*                                                                      *)
  310. (*----------------------------------------------------------------------*)
  311.  
  312. VAR
  313.    Reg  : RegPack;
  314.  
  315. BEGIN (* Write_File_Handle *)
  316.                                    (* Set parameters for write *)
  317.    WITH Reg DO
  318.       BEGIN
  319.  
  320.          Ah := $40;
  321.          Bx := File_Handle;
  322.          Cx := Write_Length;
  323.          Ds := SEG( File_Buffer );
  324.          Dx := OFS( File_Buffer );
  325.  
  326.       END;
  327.                                    (* Perform the read *)
  328.    MsDos( Reg );
  329.                                    (* Check for bad return  *)
  330.  
  331.    IF ( Carry_Flag AND Reg.Flags ) = 0 THEN
  332.       BEGIN
  333.          Write_File_Handle := 0;
  334.          Write_Length      := Reg.Ax;
  335.       END
  336.    ELSE
  337.       BEGIN
  338.          Write_File_Handle := Reg.Ax;
  339.          Write_Length      := 0;
  340.       END;
  341.  
  342. END   (* Write_File_Handle *);
  343.  
  344. (*----------------------------------------------------------------------*)
  345. (*          Create_File_Handle --- Creates file and file handle         *)
  346. (*----------------------------------------------------------------------*)
  347.  
  348. FUNCTION Create_File_Handle(     File_Name:   AnyStr;
  349.                                  File_Attrib: INTEGER;
  350.                              VAR File_Handle: INTEGER ) : INTEGER;
  351.  
  352. (*----------------------------------------------------------------------*)
  353. (*                                                                      *)
  354. (*     Function:   Create_File_Handle                                   *)
  355. (*                                                                      *)
  356. (*     Purpose:    Creates file and file handle                         *)
  357. (*                                                                      *)
  358. (*     Calling Sequence:                                                *)
  359. (*                                                                      *)
  360. (*        Error := Create_File_Handle(     File_Name:   AnyStr;         *)
  361. (*                                         File_Attrib: INTEGER;        *)
  362. (*                                     VAR File_Handle: INTEGER ) :     *)
  363. (*                                             INTEGER;                 *)
  364. (*                                                                      *)
  365. (*           File_Name   --- path name of file to be created            *)
  366. (*           File_Attrib --- attributes for file:                       *)
  367. (*                           None          = 0;                         *)
  368. (*                           Read_Only     = 1;                         *)
  369. (*                           Hidden        = 2;                         *)
  370. (*                           System        = 4;                         *)
  371. (*                           Volume_Label  = 8;                         *)
  372. (*                           Subdirectory  = 16;                        *)
  373. (*                           Archive       = 32;                        *)
  374. (*           File_Handle --- returned file handle                       *)
  375. (*           Error       --- DOS error return code                      *)
  376. (*                                                                      *)
  377. (*     Calls:                                                           *)
  378. (*                                                                      *)
  379. (*        MsDos                                                         *)
  380. (*                                                                      *)
  381. (*----------------------------------------------------------------------*)
  382.  
  383. VAR
  384.    Reg  : RegPack;
  385.  
  386. BEGIN (* Create_File_Handle *)
  387.                                    (* Convert path name to Ascii Z string *)
  388.  
  389.    Convert_String_To_AsciiZ( File_Name );
  390.  
  391.                                    (* Set parameters for create file handle *)
  392.    Reg.Cx := File_Attrib;
  393.    Reg.Ah := $3C;
  394.    Reg.Ds := SEG( File_Name[1] );
  395.    Reg.Dx := OFS( File_Name[1] );
  396.  
  397.                                    (* Open file, get handle *)
  398.    MsDos( Reg );
  399.                                    (* Check for bad return  *)
  400.  
  401.    IF ( Carry_Flag AND Reg.Flags ) = 0 THEN
  402.       BEGIN
  403.          Create_File_Handle := 0;
  404.          File_Handle        := Reg.Ax;
  405.       END
  406.    ELSE
  407.       BEGIN
  408.          Create_File_Handle := Reg.Ax;
  409.          File_Handle        := 0;
  410.       END;
  411.  
  412. END   (* Create_File_Handle *);
  413.